var category = (function() {
    
    function editCategory(cateId) {
        var name = $('#catRow_' + cateId).find('td.catName').text(),
            html = '<div class="formField" id="category_edit_form">\
                        <div class="row-fluid">\
                            <div class="span3">\
                                <span class="fLabel" for="name">Name:</span>\
                            </div>\
                            <div class="span8">\
                                <div class="iconField">\
                                    <input value="' + $.trim(name) + '" id="editCatName" class="ui-wizard-content" type="text" name="name" placeholder="name">\
                                </div>\
                            </div>\
                        </div>\
                    <div/>';
        showConfirm({
            title: 'Edit Category',
            msg: html,
            noBt: 'Cancel',
            yesBt: 'Ok',
            onYes: function() {
                var url = '/category/ajax_edit_category_name';
                $.ajax({
                    type: "POST", 
                    url: url, 
                    data: {
                        id: cateId,
                        name: $('#category_edit_form #editCatName').val()
                    }, 
                    dataType: 'json',
                    success: function(resp) {
                        if (resp.err != 0)
                            showAlert(resp.msg);
                        else {
                            $('#catRow_' + cateId).find('td.catName').html(resp.data.name);
                            $('#catRow_' + cateId).find('td.catSlug').html(resp.data.slug);
                        }
                    }
                });
            }
        });
        return false;
    }
    
    function deleteCategory(cateId) {
        var name = $('#catRow_' + cateId).find('td.catName').text(),
            html = '<br/><p>Are you sure to delete the category with name <span class="text-error">' + $.trim(name) + '</span>?<br/><br/>',
            wraning = '<p class="text-warning" align="left">Please understand that this action cannot be reverted. </p>'
        showConfirm({
            title: 'Delete Category',
            msg: html,
            warning: wraning,
            noBt: 'No, not now.',
            yesBt: 'Delete.',
            onYes: function() {
                var url = '/category/ajax_delete_category';
                $.ajax({
                    type: "POST", 
                    url: url, 
                    data: {id: cateId}, 
                    dataType: 'json',
                    success: function(resp) {
                        if (resp.err != 0)
                            showAlert(resp.msg);
                        else
                            $('#catRow_' + cateId).remove();
                    }
                });
            }
        });
        return false;
    }
    
    function addCategory_handler() {
        $('#add_new_cat_submit').click(function() {
            var name = $('#category_add_new #name').val();
            if(!name)
                return;
            $.post('/category/ajax_add_new_category', {name: name}, function(resp) {
                if (resp.err != 0) {
                    $('#name').addClass('error');
                    var msg,
                        err = resp.err;
                    switch (err) {
                        case -1:
                            msg = 'Category name not valide.';
                            break;
                        case -3:
                            msg = 'Category name exist.';
                            break;
                        default:
                            msg = 'Something wrong, please check again.';
                    }
                    $('#category_add_new #err_msg').show().find('.alert').attr('class', 'alert alert-error noMargin').html(msg);
                } else if (resp.err == 0) {
                    $('#name').removeClass('error');
                    $('#category_add_new #err_msg').show().find('.alert').attr('class', 'alert alert-success noMargin').html('Successfull.');
                    var html = '<tr>\
                                    <td>' + name + '</td>\
                                    <td>0</td>\
                                    <td align="center">\
                                    <td align="center">\
                                        <a original-title="Edit Category" href="#" rel="tipsyN">\
                                            <img src="/assets/img/icons/14x14/pen4.png" alt="">\
                                        </a>\
                                    </td>\
                                    <td align="center">\
                                        <a id="removeCate_8" class="removeCate" original-title="Delete Category" rel="" href="#">\
                                            <img src="/assets/img/icons/14x14/trash.png" alt="">\
                                        </a>\
                                    </td>\
                                </tr>';
                     $('#list_all_manga tbody').prepend(html);           
                }
            }, 'json');
            return false;
        });
    }
    
    return {
        init: function() {
            addCategory_handler();
        },
        
        editCategory: function(cateId) {
            return editCategory(cateId);
        },
        
        deleteCategory: function(cateId) {
            return deleteCategory(cateId);
        }
    };
})();

$(document).ready(function() {
    category.init();
});